package com.dykj.fireFighting.web.mq;

import com.alibaba.fastjson.JSON;
import com.dykj.fireFighting.api.elasticsearch.devHisSomke.entity.EsDevHisSomke;
import com.dykj.fireFighting.api.elasticsearch.devHisSomke.service.EsDevHisSomkeService;
import com.dykj.fireFighting.api.elasticsearch.devHisWater.entity.EsDevHisWater;
import com.dykj.fireFighting.api.elasticsearch.devHisWater.service.EsDevHisWaterService;
import com.dykj.fireFighting.common.starter.redisson.service.RedisService;
import com.dykj.fireFighting.web.entity.DevHisWater;
import com.dykj.fireFighting.web.entity.HaiManSmoke;
import com.dykj.fireFighting.web.message.InsertDevHisSmokeMessage;
import com.dykj.fireFighting.web.message.InsertDevHisWaterMessage;
import com.dykj.fireFighting.web.service.DevHisService;
import com.dykj.fireFighting.web.service.DevHisSmokeService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author zhengbo
 */
@Service
@RocketMQMessageListener(
        topic = InsertDevHisSmokeMessage.TOPIC,
        consumerGroup = "dy-consumer-group-" + InsertDevHisSmokeMessage.TOPIC
)
public class InsertDevHisSmokeConsumer implements RocketMQListener<InsertDevHisSmokeMessage> {
    public static final Logger LOGGER = LoggerFactory.getLogger(InsertDevHisSmokeConsumer.class);
    @Autowired
    private RedisService redisService;
    @DubboReference
    private EsDevHisSomkeService esDevHisSomkeService;
    @Autowired
    private DevHisSmokeService devHisSmokeService;
    @Override
    public void onMessage(InsertDevHisSmokeMessage insertDevHisSmokeMessage) {
        HaiManSmoke haiManSmoke = insertDevHisSmokeMessage.getHaiManSmoke();
        LOGGER.info("===========插入EsDevHisWater数据到mysql==============");
        devHisSmokeService.insertDevHisSmoke(haiManSmoke);
        LOGGER.info("===========插入EsDevHisWater数据到ES==============");
        EsDevHisSomke esDevHisSomke = new EsDevHisSomke();
        BeanUtils.copyProperties(haiManSmoke, esDevHisSomke);
        System.out.println("===="+esDevHisSomke.getId());
        esDevHisSomkeService.importAll(esDevHisSomke);
        LOGGER.info("===========插入EsDevHisWater数据到ES:=============" + JSON.toJSONString(esDevHisSomke));
        redisService.set(haiManSmoke.getId(), JSON.toJSONString(haiManSmoke), 3600L);
    }
}
